home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / The Timer and Time / RandomRectangle / RandomRectangle.cs next >
Encoding:
Text File  |  2001-01-15  |  1.3 KB  |  43 lines

  1. //----------------------------------------------
  2. // RandomRectangle.cs ⌐ 2001 by Charles Petzold
  3. //----------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7.  
  8. class RandomRectangle: Form
  9. {
  10.      public static void Main()
  11.      {
  12.           Application.Run(new RandomRectangle());
  13.      }
  14.      public RandomRectangle()
  15.      {
  16.           Text = "Random Rectangle";
  17.  
  18.           Timer timer    = new Timer();
  19.           timer.Interval = 1;
  20.           timer.Tick    += new EventHandler(TimerOnTick);
  21.           timer.Start();
  22.      }
  23.      void TimerOnTick(object obj, EventArgs ea)
  24.      {
  25.           Random rand = new Random();
  26.  
  27.           int x1 = rand.Next(ClientSize.Width);
  28.           int x2 = rand.Next(ClientSize.Width);
  29.           int y1 = rand.Next(ClientSize.Height);
  30.           int y2 = rand.Next(ClientSize.Height);
  31.  
  32.           Color color = Color.FromArgb(rand.Next(256), 
  33.                                        rand.Next(256), 
  34.                                        rand.Next(256));
  35.  
  36.           Graphics grfx = CreateGraphics();
  37.           grfx.FillRectangle(new SolidBrush(color), 
  38.                              Math.Min(x1,  x2), Math.Min(y1,  y2),
  39.                              Math.Abs(x2 - x1), Math.Abs(y2 - y1));
  40.           grfx.Dispose();
  41.      }
  42. }
  43.